package edu.sctu;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-5-17.
 */
public class DataBase {

    public static List<User> getUserList() throws ClassNotFoundException, SQLException {
        List<User> list = new ArrayList<User>();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        String sql = "SELECT * FROM t_users";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                "root",
                "123456"
        );
        statement = connection.createStatement();

        resultSet = statement.executeQuery(sql);

        while(resultSet.next()){

            int id = resultSet.getInt(1);
            String username = resultSet.getString(2);
            String password = resultSet.getString(3);

            //封装
            User user = new User();
            user.setId(id);
            user.setName(username);
            user.setPassword(password);

            list.add(user);
        }

        resultSet.close();
        statement.close();
        connection.close();


        return list;
    }

}
